/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package familytree.struts.actions;

import Funciones.RinconUsuario.FuncionesUtiles;
import familytree.negocio.IArbolProcessor;
import familytree.negocio.INodoProcessor;
import familytree.negocio.bo.ArbolBO;
import familytree.negocio.bo.NodoBO;
import familytree.persistencia.pojos.Nodo;
import familytree.struts.forms.BusquedaForm;
import familytree.util.ITree;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Antonio
 */
public class BusquedaAction extends Action {

    /* forward name="ok" path="" */
    private final static String OK = "ok";

    /*forward name = "fallo" path=""*/
    private final static String FALLO = "fallo";
    
    /*forward name = "volver" path = ""*/
    private final static String VOLVER = "volver";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String accion = request.getParameter("accionSeleccionada");
        IArbolProcessor arbolBO = new ArbolBO();

        if (accion.toLowerCase().equals("buscar")) {
            BusquedaForm busquedaForm = (BusquedaForm) form;
            int largo1,largo2,largo3;
            largo1 = busquedaForm.getNombre().length();
            largo2 = busquedaForm.getPrimerApellido().length();
            largo3 = busquedaForm.getSegundoApellido().length();
            if (largo1 == 0 && largo2 == 0 && largo3 == 0)
                return mapping.findForward(VOLVER);
            List<Nodo> nodos =  arbolBO.findNodosByName(busquedaForm.getNombre(),busquedaForm.getPrimerApellido(),busquedaForm.getSegundoApellido());
            request.setAttribute("resultadoBusqueda", nodos);
            return mapping.findForward(VOLVER);

        }
        else if (accion.toLowerCase().contains("enlace")){
         //Capturamos su id incluido 
            String id = FuncionesUtiles.capturaId(accion);
            
            //Capturamos el nodo completo para el id seleccionado
            
            //Hacemos de ese nodo el nodo actual
            INodoProcessor nodoBd = new NodoBO();
            Nodo nodoActual = nodoBd.getNodoPorIdNodo(id);
            ITree[] familia = (ITree[])request.getSession().getAttribute("familia"); 
            String[][] localizacion = arbolBO.getruta(nodoActual,familia);
            request.getSession().setAttribute("localizacion", localizacion);
            request.getSession().setAttribute("nodoActual", nodoActual);
            
            return mapping.findForward("ok");
        }
        if (false) {
            return mapping.findForward(FALLO);
        }
        return mapping.findForward(VOLVER);

    }
}